//
//  YTKRequest+RAC.m
//  JSBasicFramework
//
//  Created by Apple on 2019/9/12.
//  Copyright © 2019 Apple. All rights reserved.
//

#import "YTKRequest+RAC.h"

static BOOL IS_SCURE = YES;

@implementation YTKRequest (RAC)
- (RACSignal *)rac_requestSignal{
    [self stop];
    @weakify(self)
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self)
        [self startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            NSInteger code = [request.responseObject[@"code"] integerValue];
            if (code == 200 || code == 204) {
                id result = nil;
                if (!IS_SCURE) {
                    result = request.responseObject[@"value"];
                }
                [subscriber sendNext:result];
                [subscriber sendCompleted];
            }else{
                NSError *error = nil;
                if ([request.responseObject isKindOfClass:[NSDictionary class]]) {
                    error = [NSError errorWithDomain:NSURLErrorDomain code:code userInfo:@{@"message":request.responseObject[@"msg"]}];
                    
                }else{
                    error = [NSError errorWithDomain:NSURLErrorDomain code:code userInfo:@{@"message":@"未知错误"}];
                }
                if (isDebug) {
                    NSLog(@" %@ -- %@",request.baseUrl, request.error.description);
                    
                    
                }
                [subscriber sendError:error];
            }
            
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            if (isDebug) {
                NSLog(@" %@ -- %@",request.baseUrl, request.error.description);
                
               
            }
            [subscriber sendError:request.error];
        }];
        return [RACDisposable disposableWithBlock:^{
            @strongify(self)
            if (!self.isCancelled) {
                [self stop];
            }
        }];
    }];
    if (isDebug) {
        [signal setNameWithFormat:@"%@ -rac_YGRequest", NSStringFromClass([self class])];
    }
    return signal;
}

- (RACSignal *)rac_requestCacheSignal{
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        if ([self loadCacheWithError:nil]) {
            NSInteger code = [self.responseObject[@"code"] integerValue];
            if (code == 200 || code == 204) {
                id result = nil;
                if (!IS_SCURE) {
                    result = self.responseObject[@"value"];
                }
                [subscriber sendNext:result];
                [subscriber sendCompleted];
            }else{
                NSError *error = nil;
                if ([self.responseObject isKindOfClass:[NSDictionary class]]) {
                    error = [NSError errorWithDomain:NSURLErrorDomain code:code userInfo:@{@"message":self.responseObject[@"message"]}];
                    
                }else{
                    error = [NSError errorWithDomain:NSURLErrorDomain code:code userInfo:@{@"message":@"未知错误"}];
                }
                if (isDebug) {
                    NSLog(@" %@ -- %@",self.baseUrl, self.error.description);
                    
                }
                [subscriber sendError:error];
            }
        }else{
            [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:-1 userInfo:@{@"desc":@"没有缓存"}]];
        }
        
        return [RACDisposable disposableWithBlock:^{
            
        }];
    }];
}
@end
